package com.ssyt.examinationsystem.controller;

import com.ssyt.examinationsystem.dto.PaperDto;
import com.ssyt.examinationsystem.entity.RoleInfo;
import com.ssyt.examinationsystem.entity.StudentInfo;
import com.ssyt.examinationsystem.service.IPaperInfoService;
import com.ssyt.examinationsystem.service.IRoleInfoService;
import com.ssyt.examinationsystem.service.IScoreInfoService;
import com.ssyt.examinationsystem.service.IStudentInfoService;
import com.ssyt.examinationsystem.service.impl.PaperInfoServiceImpl;
import com.ssyt.examinationsystem.service.impl.RoleInfoServiceImpl;
import com.ssyt.examinationsystem.service.impl.ScoreInfoServiceImpl;
import com.ssyt.examinationsystem.service.impl.StudentInfoServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.URLDecoder;
import java.util.List;
import java.util.Map;

/**
 * Servlet implementation class StudentInfoController
 */
public class StudentInfoController extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public StudentInfoController() {
        super();
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
     * response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doPost(request, response);
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
     * response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        // 创建一个业务对象
        IStudentInfoService stuService = new StudentInfoServiceImpl();
        // 创建一个试卷业务对象
        IPaperInfoService paperService = new PaperInfoServiceImpl();
        // 创建一个角色业务对象
        IRoleInfoService roleService = new RoleInfoServiceImpl();
        // 创建一个成绩业务对象
        IScoreInfoService scoreService = new ScoreInfoServiceImpl();
        // 接收输出流对象
        PrintWriter out = response.getWriter();
        // 得到地址栏上的参数
        String flag = request.getParameter("flag");
        if ("list".equals(flag)) {
            list(stuService, request, response);
        } else if ("delete".equals(flag)) {
            delete(stuService, request, response, out);
        } else if ("initUpdate".equals(flag)) {
            initUpdate(roleService, stuService, request, response);
        } else if ("update".equals(flag)) {
            update(stuService, request, response, out);
        } else if ("initAdd".equals(flag)) {
            initAdd(roleService, stuService, request, response);
        } else if ("add".equals(flag)) {
            add(stuService, request, response, out);
        } else if ("get".equals(flag)) {
            getStu(roleService, stuService, request, response, out);
        } else if ("lead".equals(flag)) {
            lead(request, response);
        } else if ("stuScore".equals(flag)) {
            stuScore(paperService, stuService, scoreService, request, response);
        }
        out.flush();
        out.close();
    }

    /**
     * 根据学生编号查询成绩
     *
     * @param scoreService
     * @param request
     * @param response
     * @throws IOException
     * @throws ServletException
     */
    private void stuScore(IPaperInfoService paperService, IStudentInfoService stuService,
                          IScoreInfoService scoreService, HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        // 获取学生编号
        String stuId = String.valueOf(request.getSession().getAttribute("stuId"));
        // 调用根据学生编号获取成绩
        Map<String, Object> score = scoreService.getScoreByStuId(stuId);
        float totalScore = Float.valueOf(String.valueOf(score.get("objectiveScore")))
                + Float.valueOf(String.valueOf(score.get("subjectiveScore")));
        score.put("totalScore", totalScore);

        // 调用根据学生编号获取学生
        Map<String, Object> stu = stuService.getStuByStuId(stuId);
        // 调用根据试卷编号获取试卷
        String paperId = String.valueOf(score.get("paperId"));
        PaperDto paperDto = paperService.getPaperInfoById(paperId);
        Map<String, Object> paper = paperDto.getPaper();

        request.setAttribute("paper", paper);
        request.setAttribute("score", score);
        request.setAttribute("stu", stu);
        // 转发到学生查询成绩页面
        request.getRequestDispatcher("WEB-INF/views/student/stuScore.jsp").forward(request, response);
    }

    /**
     * 转发到导入学生信息页面
     *
     * @param request
     * @param response
     * @throws IOException
     * @throws ServletException
     */
    private void lead(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.getRequestDispatcher("WEB-INF/views/student/stuLead.jsp").forward(request, response);
    }

    /**
     * 根据学生账号密码查询学生个人信息
     *
     * @param stuService
     * @param request
     * @param response
     * @param out
     * @throws IOException
     * @throws ServletException
     */
    private void getStu(IRoleInfoService roleService, IStudentInfoService stuService, HttpServletRequest request,
                        HttpServletResponse response, PrintWriter out) throws ServletException, IOException {
        // 获取登录用户的密码，账号和角色编号
        String account = String.valueOf(request.getSession().getAttribute("account"));
        String password = String.valueOf(request.getSession().getAttribute("password"));
        String roleId = String.valueOf(request.getSession().getAttribute("roleId"));
        // 调用根据密码，账号查询学生信息的业务方法
        Map<String, Object> mapStu = stuService.getStuByAcc(account, password);
        // 调用根据角色编号查询角色对象的业务方法
        Map<String, Object> role = roleService.getRoleByRoleId(roleId);
        // 讲角色和学生对象保存到请求对象中
        request.setAttribute("role", role);
        request.setAttribute("mapStu", mapStu);
        // 转发到WEB-INF/views/student/stuAcc.jsp页面
        request.getRequestDispatcher("WEB-INF/views/student/stuAcc.jsp").forward(request, response);

    }

    /**
     * 添加学生的方法
     *
     * @param stuService
     * @param request
     * @param response
     * @param out
     * @throws IOException
     */
    private void add(IStudentInfoService stuService, HttpServletRequest request, HttpServletResponse response,
                     PrintWriter out) throws IOException {
        // 获取表单数据
        String stuName = request.getParameter("stuName");
        // 对网页数据进行转码
        stuName = URLDecoder.decode(stuName, "UTF-8");

        String account = request.getParameter("account");
        account = URLDecoder.decode(account, "UTF-8");

        String password = request.getParameter("password");
        password = URLDecoder.decode(password, "UTF-8");

        String stuClass = request.getParameter("stuClass");
        stuClass = URLDecoder.decode(stuClass, "UTF-8");

        String roleId = request.getParameter("roleId");
        roleId = URLDecoder.decode(roleId, "UTF-8");
        // 创建一个学生对象，封装属性
        StudentInfo stu = new StudentInfo();
        stu.setAccount(account);
        stu.setPassword(password);
        stu.setStuClass(stuClass);
        stu.setStuName(stuName);
        RoleInfo role = new RoleInfo();
        role.setRoleId(Integer.valueOf(roleId));
        stu.setRole(role);
        if (stuService.addStudentInfo(stu) > 0) {
            out.print("ok");
        }

    }

    /**
     * 初始化添加的方法
     *
     * @param stuService
     * @param request
     * @param response
     * @throws IOException
     * @throws ServletException
     */
    private void initAdd(IRoleInfoService roleService, IStudentInfoService stuService, HttpServletRequest request,
                         HttpServletResponse response) throws ServletException, IOException {
        // 获取所有角色信息
        List<RoleInfo> roleList = roleService.getRoleInfos();
        request.setAttribute("roleList", roleList);
        // 转发到WEB-INF/views/student/stuAdd.jsp页面
        request.getRequestDispatcher("WEB-INF/views/student/stuAdd.jsp").forward(request, response);
    }

    /**
     * 修改学生信息的方法
     *
     * @param stuService
     * @param request
     * @param response
     * @param out
     * @throws IOException
     */
    private void update(IStudentInfoService stuService, HttpServletRequest request, HttpServletResponse response,
                        PrintWriter out) throws IOException {
        // 获取表单数据
        String stuId = request.getParameter("stuId");
        // 对网页数据进行转码
        stuId = URLDecoder.decode(stuId, "UTF-8");
        String stuName = request.getParameter("stuName");
        stuName = URLDecoder.decode(stuName, "UTF-8");

        String account = request.getParameter("account");
        account = URLDecoder.decode(account, "UTF-8");

        String password = request.getParameter("password");
        password = URLDecoder.decode(password, "UTF-8");

        String stuClass = request.getParameter("stuClass");
        stuClass = URLDecoder.decode(stuClass, "UTF-8");

        String roleId = request.getParameter("roleId");
        roleId = URLDecoder.decode(roleId, "UTF-8");
        // 创建一个学生对象，封装属性
        StudentInfo stu = new StudentInfo();
        stu.setStuId(Integer.valueOf(stuId));
        stu.setAccount(account);
        stu.setPassword(password);
        stu.setStuClass(stuClass);
        stu.setStuName(stuName);
        RoleInfo role = new RoleInfo();
        role.setRoleId(Integer.valueOf(roleId));
        stu.setRole(role);
        // 调用修改的业务方法
        if (stuService.updateStudentInfo(stu) > 0) {
            out.print("ok");
        }
    }

    /**
     * 初始化修改学生信息的方法
     *
     * @param stuService
     * @param request
     * @param response
     * @throws IOException
     * @throws ServletException
     */
    private void initUpdate(IRoleInfoService roleService, IStudentInfoService stuService, HttpServletRequest request,
                            HttpServletResponse response) throws ServletException, IOException {
        // 获取学生编号
        String stuId = request.getParameter("stuId");
        // 获取角色编号
        String roleId = request.getParameter("roleId");
        // 调用根据学生编号得到学生对象
        Map<String, Object> stu = stuService.getStuByStuId(stuId);
        request.setAttribute("stu", stu);

        // 调用根据角色编号查询角色对象的业务方法
        Map<String, Object> role = roleService.getRoleByRoleId(roleId);
        request.setAttribute("role", role);
        // 转发到WEB-INF/views/student/stuUpdate.jsp页面中
        request.getRequestDispatcher("WEB-INF/views/student/stuUpdate.jsp").forward(request, response);
    }

    /**
     * 根据学生编号删除学生信息
     *
     * @param stuService
     * @param request
     * @param response
     * @param out
     */
    private void delete(IStudentInfoService stuService, HttpServletRequest request, HttpServletResponse response,
                        PrintWriter out) {
        // 获取学生编号
        String stuId = request.getParameter("stuId");
        // 调用根据学生编号删除学生信息的业务方法
        int row = stuService.deleteStudentInfo(stuId);
        if (row > 0) {
            out.print("ok");
        }
    }

    /**
     * 查询所有学生的方法
     *
     * @param stuService
     * @param request
     * @param response
     * @throws IOException
     * @throws ServletException
     */
    private void list(IStudentInfoService stuService, HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        // 调用查询所有的业务方法
        List<StudentInfo> stuList = stuService.getStudentInfos();
        // 将集合放入请求对象中
        request.setAttribute("stuList", stuList);
        // 转发到WEB-INF/views/student/stuList.jsp页面中
        request.getRequestDispatcher("WEB-INF/views/student/stuList.jsp").forward(request, response);
    }

}
